/**
 * 
 */
package travibot.utility;

import java.io.FileWriter;
import java.io.Serializable;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

/**
 * @author tajzivit
 *
 */
public class VelocityUtil implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -5396888771540180116L;

    public static final String PROPERTY_LOADER_PATH = "file.resource.loader.path";
    
    private String templateFolderPath;
    private VelocityEngine velocityEngine = new VelocityEngine();
    private FileWriter writer;
    private String outputFilePath;
    private VelocityContext context = new VelocityContext();
    
    
    public String getTemplateFolderPath() {
    
        return templateFolderPath;
    }
    
    public void setTemplateFolderPath(String templateFolderPath) {
    
        this.templateFolderPath = templateFolderPath;
        velocityEngine.setProperty(PROPERTY_LOADER_PATH, this.templateFolderPath);
    }

    
    /**
     * default contructor
     * 
     * @param outputFilePath output file path
     */
    public VelocityUtil(String outputFilePath) {

        super();
        this.outputFilePath = outputFilePath;
        
        initVelocityEngine();
    }

    /**
     * 
     * @param outputFilePath output file path
     * @param templateFolderPath folder where are templates
     */
    public VelocityUtil(String outputFilePath, String templateFolderPath){
        
        super();
        this.outputFilePath = outputFilePath;
        this.setTemplateFolderPath(templateFolderPath);
        
        initVelocityEngine();
    }
    
    /**
     * initialize velocity engine
     *
     */
    private void initVelocityEngine(){
        
        try{
            velocityEngine.init();
        }
        catch (Exception e) {
            // TODO: handle exception
        }
    }
    
    /**
     * adds value to velocity context - a variable in the 
     * template will be replace by this one
     * 
     * @param key varibale's name (in the template)
     * @param value variable's value
     */
    public void addValueToContext(String key, Object value){
        
        context.put(key, value);
    }
    
    /**
     * generate merged template to the file
     * 
     * @param templateName template to merge
     * @throws Exception
     */
    public void generate(String templateName)throws Exception{
        
        writer = new FileWriter(this.outputFilePath);
        
        Template template = velocityEngine.getTemplate(templateName);
        
        template.merge(context, writer);
        
        writer.flush();
        writer.close();
    }
    
    /**
     * generate merged template to specific output file
     * 
     * @param templateName template to merge
     * @param outputFilePath file to save
     * @throws Exception
     */
    public void generate(String templateName, String outputFilePath)throws Exception{
        
        
        this.outputFilePath = outputFilePath;
        
        this.generate(templateName);
    }
    
    /**
     * merges template to outputFile by the specific template, context.
     * templates searching in the default directory "./";
     * 
     * @param context velocity context
     * @param templateName template name
     * @param outputFilePath output file
     * @throws Exception
     */
    public static void generate(VelocityContext context, String templateName, String outputFilePath)
    throws Exception{
        
       VelocityUtil velocityUtil = new VelocityUtil(outputFilePath);
       velocityUtil.context = context;
       velocityUtil.generate(templateName);
    }
    
    /**
     * merges template to outputFile by the specific template, context.
     * templates searching in the specific directory
     * 
     * @param context
     * @param templateFolder
     * @param templateName
     * @param outputFilePath
     * @throws Exception
     */
    public static void generate(VelocityContext context, String templateFolder, String templateName, String outputFilePath)
    throws Exception{
        
        VelocityUtil velocityUtil = new VelocityUtil(outputFilePath, templateFolder);
        velocityUtil.context = context;
        velocityUtil.generate(templateName);
    }
}
